package main;

import java.awt.event.WindowListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.swing.JOptionPane;

import model.DrawingModel;
import model.NoFileRTE;

public class GUIManager
{
	private static GUIManager modelMgr;
	
	Map<String, DrawingModel> models;
	
	
	private GUIManager()
	{
		models = new HashMap<String, DrawingModel>();
	}
	
	public static GUIManager getInstance()
	{
		if (modelMgr == null)
			modelMgr = new GUIManager();
		
		return modelMgr;
	}
	
		
	public void addGUI(String filename)
	{
		DrawingModel mod = new DrawingModel();
		
		if (modelExists(filename))
		{
			mod = getExistingModel(filename);
		}
		else
		{
			mod = loadModelFile(filename);
			if (mod == null)
				return;
			else
				models.put(filename, mod);
		}

		GUI tmpGUI = new GUI(mod);
		tmpGUI.instantiate();		
	}
	
	private DrawingModel getExistingModel(String filename) 
	{
		return models.get(filename);
	}

	public boolean modelExists(String filename)
	{		
//		DrawingModel mod = new DrawingModel();
//		mod.setFileName(filename);
		
		if (models.containsKey(filename))
			return true;
		
		return false;
	}
	
	private DrawingModel loadModelFile(String filename)
	{
		DrawingModel mod;
		if (filename.equals(""))
			mod = new DrawingModel();
			
		try
		{
			mod = new DrawingModel(filename);
		}
		catch (NoFileRTE e)
		{ 
			JOptionPane.showMessageDialog(null, "Error on load: " + e.getMessage() );
			System.out.println( "Error on load: " + e.getMessage() );
			return null;
		}
		
		return mod;
	}

	public void addModel(DrawingModel mod) 
	{
		models.put(mod.getFileName(), mod);
	}
	
	public void removeModel(DrawingModel mod)
	{
		models.remove(mod.getFileName());
	}
	
	
}
